package com.module_live.fragment.coachstudy;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.ui.fragment.BaseAppFragment;
import com.module_live.R;
import com.module_live.R2;
import com.module_live.activity.CoachStudyLivingActivity;
import com.module_live.bean.MyTrackInfo;
import com.qiniu.droid.rtc.QNSurfaceView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class StudyUserListFragment extends BaseAppFragment {

    @BindView(R2.id.rv_student)
    RecyclerView rvUserList;

    @BindView(R2.id.rv_teacher)
    RecyclerView rvTeacher;

    @BindView(R2.id.qn_bigvideo)
    QNSurfaceView qnBigvideo;

    private StudentHomeAdapter studentHomeAdapter;
    private GridLayoutManager gridLayoutManager;

    private TeacherHomeAdapter teacherHomeAdapter;

    private List<MyTrackInfo> studentList = new ArrayList<>();
    private List<MyTrackInfo> teacherList = new ArrayList<>();

    @Override
    protected void onCreateView(View view, Bundle savedInstanceState) {

        initStudentRecyclerView();
//        initTeacherRecyclerView();

        qnBigvideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (qnBigvideo.getVisibility() == View.VISIBLE) {

                    qnBigvideo.setVisibility(View.GONE);
                    rvUserList.setVisibility(View.VISIBLE);
                    if (studentHomeAdapter != null) {
                        studentHomeAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

    }

    public void initStudentRecyclerView() {

        if (rvUserList != null) {

            studentHomeAdapter = new StudentHomeAdapter(getActivity(), studentList);
            gridLayoutManager = new GridLayoutManager(getActivity(), 3);
            rvUserList.setLayoutManager(gridLayoutManager);
            rvUserList.setAdapter(studentHomeAdapter);
        }

    }

//    public void initTeacherRecyclerView() {
//
//        if (rvTeacher != null) {
//
//            teacherHomeAdapter = new TeacherHomeAdapter(getActivity(), teacherList);
//            gridLayoutManager = new GridLayoutManager(getActivity(), 3);
//            rvTeacher.setLayoutManager(gridLayoutManager);
//            rvTeacher.setAdapter(studentHomeAdapter);
//        }
//
//    }

//    public void setTeacherTrackInfo(List<MyTrackInfo> trackInfoList) {
////
////        teacherList.clear();
////        teacherList.addAll(trackInfoList);
////
////        LogUtil.e("=====================>直播间老师和学管人数为：" + teacherList.size());
////
////        if (teacherHomeAdapter != null) {
////            teacherHomeAdapter.notifyDataSetChanged();
////        }
////    }


    public void setStudentTrackInfo(List<MyTrackInfo> trackInfoList) {

        studentList.clear();
        studentList.addAll(trackInfoList);

        LogUtil.e("=====================>直播间学生和学管人数为：" + studentList.size());

        if (studentHomeAdapter != null) {
            studentHomeAdapter.notifyDataSetChanged();
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_study_userlist;
    }


    public class StudentHomeAdapter extends BaseAdapter<MyTrackInfo> {

        public StudentHomeAdapter(Context context, List<MyTrackInfo> mData) {
            super(context, mData);

        }

        @Override
        public int getItemCount() {
            return 45;
        }

        @Override
        public int getLayoutId() {

            return R.layout.item_study_user;
        }

        @Override
        protected void toBindViewHolder(final ViewHolder holder, final int position, final List<MyTrackInfo> mData) {

            LinearLayout linBg = holder.getView(R.id.lin_bg);
            FrameLayout frView = holder.getView(R.id.fr_surfaceview);
            LinearLayout linItem = holder.getView(R.id.item_parent);

            //如果position与集合长度相等，代表都是空的，显示图片
            if (position == mData.size() || position > mData.size()) {
                linBg.setVisibility(View.VISIBLE);
                frView.setVisibility(View.GONE);

            } else {

                LogUtil.e("当前的position位置为：" + position + "，获取到的视频流为：" + mData.get(position).getVideoTrack()
                        + "，获取到的语音流为：" + mData.get(position).getAudioTrack());

                linBg.setVisibility(View.GONE);
                frView.setVisibility(View.VISIBLE);

                //如果当前为老师音视频信息，则不显示在列表中

                if (mData.get(position).getUserId().equals(CoachStudyLivingActivity.teacherPhone)) {

                    LogUtil.e("老师的id为：" + mData.get(position).getUserId()
                            + "，老师的视频流为：" + mData.get(position).getVideoTrack());

                    if (listener != null) {
                        listener.onFindAdmin(mData.get(position));
                    }
//                    linItem.setVisibility(View.GONE);
                    mData.remove(position);
                }

                if (mData.size() > 0) {
                    final QNSurfaceView qn_video = holder.getView(R.id.qn_video);
                    holder.setText(R.id.tv_userName, mData.get(position).getUserName());

                    qn_video.setZOrderOnTop(false);
                    qn_video.setZOrderMediaOverlay(false);

                    LogUtil.e("学生的视频流为：" + mData.get(position).getVideoTrack());
                    if (listener != null) {
                        listener.onSetQNSurfaceView(mData.get(position), qn_video);
                    }

                    qn_video.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            if ("1".equals(SharedPreferenceManager.getInstance(getActivity()).getUserCache().getUserIdentity())) {

                                if (qnBigvideo.getVisibility() == View.GONE) {

                                    rvUserList.setVisibility(View.GONE);
                                    qnBigvideo.setVisibility(View.VISIBLE);

                                    if (listener != null) {
                                        listener.onSetQNSurfaceView(mData.get(position), qnBigvideo);
                                    }
                                }
                            }
                        }
                    });
                }
            }
        }
    }


    public class TeacherHomeAdapter extends BaseAdapter<MyTrackInfo> {

        public TeacherHomeAdapter(Context context, List<MyTrackInfo> mData) {
            super(context, mData);

        }

        @Override
        public int getItemCount() {
            return 3;
        }

        @Override
        public int getLayoutId() {

            return R.layout.item_study_user;
        }

        @Override
        protected void toBindViewHolder(final ViewHolder holder, final int position, List<MyTrackInfo> mData) {

            LinearLayout linBg = holder.getView(R.id.lin_bg);
            FrameLayout linView = holder.getView(R.id.fr_surfaceview);

            //如果position与集合长度相等，代表都是空的，显示图片
            if (position == mData.size() || position > mData.size()) {
                linBg.setVisibility(View.VISIBLE);
                linView.setVisibility(View.GONE);

            } else {

                LogUtil.e("当前的position位置为：" + position + "，获取到的视频流为：" + mData.get(position).getVideoTrack()
                        + "，获取到的语音流为：" + mData.get(position).getAudioTrack());

                linBg.setVisibility(View.GONE);
                linView.setVisibility(View.VISIBLE);

//                holder.setVisibility(true);

                final QNSurfaceView qn_video = holder.getView(R.id.qn_video);

                holder.setText(R.id.tv_userName, mData.get(position).getUserId());
                TextView tvTime = holder.getView(R.id.tv_time);

                qn_video.setZOrderOnTop(false);
                qn_video.setZOrderMediaOverlay(false);

                if (mData.get(position).getUserId().equals("")) {

                    if (listener != null) {
                        listener.onFindAdmin(studentList.get(position));
                    }

                } else {

                    if (listener != null) {
                        listener.onSetQNSurfaceView(studentList.get(position), qn_video);

                    }
                }

            }
        }
    }

    public interface StudyUserListFragmentListener {

        void onFindAdmin(MyTrackInfo trackInfo);

        void onSetQNSurfaceView(MyTrackInfo trackInfo, QNSurfaceView qnSurfaceView);
    }

    StudyUserListFragmentListener listener;

    public void setStudyUserListFragmentListener(StudyUserListFragmentListener studyUserListFragmentListener) {

        this.listener = studyUserListFragmentListener;
    }

}
